/**
 * RefundRequestDaoImpl.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月21日
 */
package com.ovt.order.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.ovt.common.utils.StringUtils;
import com.ovt.order.dao.contant.OrderState;
import com.ovt.order.dao.mapper.RefundReasonMapper;
import com.ovt.order.dao.mapper.RefundRequestMapper;
import com.ovt.order.dao.vo.RefundReason;
import com.ovt.order.dao.vo.RefundRequest;
import com.ovt.order.service.exception.ServiceException;

/**
 * RefundRequestDaoImpl
 * 
 * @Author brad.zhou
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
@Repository
public class RefundRequestDaoImpl implements RefundRequestDao
{
    @Autowired
    private DaoHelper daoHelper;

    @Autowired
    private RefundRequestMapper refundRequestMapper;

    @Autowired
    RefundReasonMapper refundReasonMapper;

    public static final String SQL_INSERT_REFUND_REQUEST = "INSERT INTO REFUND_REQUEST(order_no, "
            + "refund_reason_id, refund_fee,"
            + "refund_desc, refund_state) "
            + "VALUES(?, ?, ?, ?, ?)";

    public static final String SQL_GET_REFUND_REQUEST_LIST = "SELECT id, order_no, refund_reason_id, "
            + "refund_fee, refund_desc, refund_state FROM refund_request";

    public static final String SQL_GET_REFUND_REQUEST_LIST1 = "SELECT id, order_no, refund_reason_id, "
            + "refund_fee, refund_desc, refund_state FROM refund_request WHERE id IN ({0})";

    public static final String SQL_UPDATE_REFUND_REQUEST_LIST_STATE = "UPDATE refund_request SET refund_state = ?"
            + "WHERE id IN ({0})";
    
    public static final String SQL_UPDATE_REFUND_REQUEST_LIST = "UPDATE refund_request SET refund_state = ?, auditor_id = ?"
            + "WHERE id IN ({0})";
    
    public static final String SQL_UPDATE_REFUND_REQUEST_LIST1 = "UPDATE refund_request SET refund_state = ?, auditor_id = ?, feedback = ?"
            + "WHERE id = ?";

    public static final String SQL_INSERT_REFUND_REASON = "INSERT INTO REFUND_REASON(refund_reason)"
            + "VALUES(?)";

    public static final String SQL_GET_REFUND_REASON_LIST = "SELECT id, reason FROM refund_reason";

    public static final String SQL_UPDATE_REFUND_REASON = "UPDATE refund_reason SET reason = ?"
            + "WHERE id = ?";

    public static final String SQL_DELETE_REFUND_REASON = "DELETE FROM refund_reason WHERE id = ?";

    public static final String SQL_UPDATE_REFUND_AUDITOR_ID = "UPDATE refund_request SET auditor_id = ?"
            + "WHERE id IN ({0})";

    @Override
    public String saveRefundRequest(RefundRequest refundRequest)
    {
        Object[] params = new Object[5];
        params[0] = refundRequest.getOrderNo();
        params[1] = refundRequest.getRefundReasonId();
        params[2] = refundRequest.getRefundFee();
        params[3] = refundRequest.getRefundDesc();
        params[4] = refundRequest.getRefundState().toString();

        String errMsg = MessageFormat.format(
                "Failed to insert refundRequest, orderNo={0}!",
                refundRequest.getOrderNo());

        daoHelper.save(SQL_INSERT_REFUND_REQUEST, null, errMsg, true, params);

        return refundRequest.getOrderNo();
    }

    @Override
    public List<RefundRequest> queryAllRefundRequestList()
    {
        String errMsg = "Failed to query reqund request list!";
        Object[] objects = null;
        List<RefundRequest> refundRequests = daoHelper.queryForList(
                SQL_GET_REFUND_REQUEST_LIST, refundRequestMapper, errMsg,
                objects);

        return refundRequests;
    }

    @Override
    public List<RefundRequest> queryRefundRequestList(List<Long> requestIdList)
    {
        String sql = MessageFormat.format(SQL_GET_REFUND_REQUEST_LIST1,
                StringUtils.getCSV(requestIdList));

        String errMsg = "Failed to query refundRequest list";
        List<RefundRequest> refundRequestList = daoHelper.queryForList(sql,
                refundRequestMapper, errMsg);

        return refundRequestList;
    }

    @Override
    public OrderState updateRefundRequestListState(List<Long> requestListId,
            OrderState state)
    {
        String sql = MessageFormat.format(SQL_UPDATE_REFUND_REQUEST_LIST_STATE,
                StringUtils.getCSV(requestListId));
        String errMsg = "Failed to update refundRequest list's state!";
        daoHelper.update(sql, null, errMsg, state);
        return state;
    }

    @Override
    public void saveRefundReason(RefundReason refundReason)
    {
        String errMsg = MessageFormat.format(
                "Failed to insert refundReason[{0}]!", refundReason);

        daoHelper.save(SQL_INSERT_REFUND_REASON, null, errMsg, true,
                refundReason);
    }

    @Override
    public List<RefundReason> queryRefundReasonList()
    {
        String errMsg = "Failed to query reqund reason list!";
        List<RefundReason> refundReasons = daoHelper.queryForList(
                SQL_GET_REFUND_REASON_LIST, refundReasonMapper, errMsg);

        return refundReasons;
    }

    @Override
    public String updateRefundReason(long id, String reason)
    {
        Object[] params = new Object[2];
        params[0] = reason;
        params[1] = id;
        String errMsg = MessageFormat.format(
                "Failed to update refundReason [{0}]!", reason);
        daoHelper.update(SQL_UPDATE_REFUND_REASON, null, errMsg, params);
        return reason;
    }

    @Override
    public void deleteRefundReason(long id)
    {

        String errMsg = MessageFormat.format(
                "Failed to delete refundRequest [{0}] state!", id);
        daoHelper.delete(SQL_DELETE_REFUND_REASON, null, errMsg, id);

    }

    @Override
    public void updateAuditorId(List<Long> requestListId, Long auditorId)
            throws ServiceException
    {
        String sql = MessageFormat.format(SQL_UPDATE_REFUND_AUDITOR_ID,
                StringUtils.getCSV(requestListId));
        String errMsg = "Failed to update refundRequest list's auditorId!";
        daoHelper.update(sql, null, errMsg);
    }

    /* (non-Javadoc)
     * @see com.ovt.dao.RefundRequestDao#updateRefundRequestList(java.util.List, com.ovt.dao.contant.OrderState, long)
     */
    @Override
    public OrderState updateRefundRequestList(List<Long> requestListId,
            OrderState state, long auditorId)
    {
        String sql = MessageFormat.format(SQL_UPDATE_REFUND_REQUEST_LIST,
                StringUtils.getCSV(requestListId));
        String errMsg = "Failed to update refundRequest list!";
        daoHelper.update(sql, null, errMsg, state, auditorId);
        return state;
    }

    /* (non-Javadoc)
     * @see com.ovt.dao.RefundRequestDao#updateRefundRequestList(java.util.List, com.ovt.dao.contant.OrderState, long, java.lang.String)
     */
    @Override
    public OrderState updateRefundRequestList(final List<Long> requestListId,
            final OrderState state, final long auditorId, final List<String> feedback)
    {
        daoHelper.saveBatch(SQL_UPDATE_REFUND_REQUEST_LIST1,
                "Failed to update refundRequest list!",
                new BatchPreparedStatementSetter()
                {

                    @Override
                    public void setValues(PreparedStatement ps, int i)
                            throws SQLException
                    {
                        ps.setString(1, state.toString());
                        ps.setLong(2, auditorId);
                        ps.setString(3, feedback.get(i));
                        ps.setFloat(4, requestListId.get(i));
                    }

                    @Override
                    public int getBatchSize()
                    {
                        return requestListId.size();
                    }
                });
        
        return state;
    }

}
